package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketAdmin;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.bean.pojo.MarketUser;

import com.cskaoyan.bean.vo.*;

import com.cskaoyan.bean.vo.CollectListVO;
import com.cskaoyan.bean.vo.FeedbackListVO;
import com.cskaoyan.bean.vo.UserListVO;

import com.cskaoyan.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 后台-用户管理模块
 * 创建日期: 2022/04/06 16:19
 *
 * @author
 */
@RestController
@RequestMapping("admin")
public class UserController {

    @Autowired
    UserService userService;

    // 会员管理：/admin/user/list ----------------------------------
    @RequestMapping("user/list")
    public BaseRespVo userList(BaseParam baseParam,String username,String mobile){
        UserListVO userListVO = userService.userList(baseParam,username,mobile);
        return BaseRespVo.ok(userListVO);
    }

    @RequestMapping("user/detail")
    public BaseRespVo userDetail(Integer id){
        MarketUser marketUser = userService.userDetail(id);
        return BaseRespVo.ok(marketUser);
    }

    /**
     * 罗旭欣  增加了更新用户信息的方法
     *
     * @param
     * @return
     */

    @PostMapping("user/update")
    public BaseRespVo updateUser(@RequestBody @Validated MarketUser marketUser, BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            String field = fieldError.getField();
            if ("nickname".equals(field)) {
                return BaseRespVo.fail(1000, "用户名长度最少为5位");
            }
            if ("password".equals(field)) {
                return BaseRespVo.fail(1000, "密码长度不符合要求");
            }
            if ("mobile".equals(field)) {
                return BaseRespVo.fail(1000, "用户手机号为11位");
            }

        }
        int count = userService.updateUser(marketUser);
        return BaseRespVo.ok(count);
    }

    // 收货地址：/admin/address/list ------------------------------------
    @RequestMapping("address/list")
    public BaseRespVo addressList(@Validated BaseParam param, String name, Integer userId) {

        UserAddressListVO userAddressListVO = userService.queryAddressList(param, name, userId);
        return BaseRespVo.ok(userAddressListVO);
    }

    // 会员收藏：/admin/collect/list ------------------------------------
    @RequestMapping("collect/list")
    public BaseRespVo collectList(Integer page, Integer limit, String sort, String order,Integer userId,Integer valueId) {
        CollectListVO collectListVO = userService.collectList(page, limit, sort, order,userId,valueId);

        return BaseRespVo.ok(collectListVO);
    }


    // 会员足迹：/admin/footprint/list ------------------------------------
    @RequestMapping("footprint/list")
    public BaseRespVo footprintList(BaseParam baseParam,Integer userId,Integer goodsId){
        FootprintListVO footprintListVOList = userService.footprintList(baseParam,userId,goodsId);
        return BaseRespVo.ok(footprintListVOList);
    }

    // 搜索历史：/admin/history/list ------------------------------------
    @RequestMapping("history/list")
    public BaseRespVo historyList(BaseParam param, String keyword, Integer userId) {
        UserHistoryListVO userHistoryListVO = userService.queryHistoryList(param, keyword, userId);
        return BaseRespVo.ok(userHistoryListVO);
    }


    // 意见反馈：/admin/feedback/list ------------------------------------
    @RequestMapping("feedback/list")
    public BaseRespVo feedbackList(Integer page, Integer limit, String username, String sort, String order, Integer id) {
        FeedbackListVO feedbackListVO = userService.feedbackList(page, limit, username, order, sort, id);
        return BaseRespVo.ok(feedbackListVO);
    }

    // 一个配置: /admin/profile/nnotice
    @RequestMapping("profile/nnotice")
    public BaseRespVo profileNnotice() {
        return BaseRespVo.ok();
    }

}
